<template>
  <div class="loginbody">
    <div class="logindata">
      <div class="logintext">
        <h2>Welcome</h2>
      </div>
      <div class="formdata">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="mainUserName">
            <el-input
              v-model="form.mainUserName"
              clearable
              placeholder="请输入主账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="secondUserName">
            <el-input
              v-model="form.secondUserName"
              clearable
              placeholder="请输入子账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              clearable
              placeholder="请输入密码"
              show-password
              @keyup.native.enter="login('form')"
            ></el-input>
          </el-form-item>
          <el-form-item prop="projectName">
            <el-input
              v-model="form.projectName"
              clearable
              placeholder="请输入任务名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="tool">
        <div>
          <el-checkbox v-model="checked" @change="remenber"
            >记住密码</el-checkbox
          >
        </div>
        <div>
          <span class="shou" @click="forgetpas">忘记密码？</span>
        </div>
      </div>
      <div class="butt">
        <el-button type="primary" @click.native.prevent="login('form')"
          >登录</el-button
        >
        <el-button class="shou" @click="register">注册</el-button>
      </div>
    </div>
  </div>
</template>
  
  <script>
import { postAuthTokens } from "@/api/configHttpBigModelLogin";
// import Cookies from 'js-cookie'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "login",
  data() {
    return {
      inputTextBaseData: this.$route.params.baseData.trim(),
      form: {
        password: "",
        mainUserName: "", //请输入主账号
        secondUserName: "", // 请输入子账号
        projectName: "", // 请输入任务名称project_name
        project_id:'',// project_id项目id
        assistant_id:''//assistant_id 助手id
        //user name、domain name
      },
      checked: false,
      rules: {
        mainUserName: [
          { required: true, message: "请输入主账号", trigger: "blur" },
          //   { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        secondUserName: [
          { required: true, message: "请输入子账号", trigger: "blur" },
          //   { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          //   { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          //   { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    if (localStorage.getItem("user")) {
      this.form.mainUserName = JSON.parse(
        localStorage.getItem("user")
      ).mainUserName;
      this.checked = true;
    }
  },
  methods: {
    login(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const { password, mainUserName, secondUserName, projectName } =
            this.form;
          let pamars = {
            auth: {
              identity: {
                password: {
                  user: {
                    domain: {
                      name: mainUserName,
                    },
                    name: secondUserName,
                    password: password,
                  },
                },
                methods: ["password"],
              },
              scope: {
                project: {
                  name: projectName,
                },
              },
            },
          };
          console.log(pamars, "pamars");

          //   this.$router.push({
          //     path: "/home",
          //   });
          postAuthTokens(pamars)
            .then((response) => {
              console.log(response, "response");
              let headerCookie = response["x-subject-token"];
              localStorage.setItem("token", headerCookie);
            })
            .then((res) => {
              console.log(res, "res");
              if (res.code === 200) {
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
                this.$emit("login");
                this.$router.push({
                  path: "/home",
                });
                localStorage.setItem("user", JSON.stringify(this.form));
              } else {
                this.$message.error("用户名或密码错误");
              }
            })
            // eslint-disable-next-line no-unused-vars
            .catch((e) => {
              localStorage.setItem("token", "zheshitoken");
              this.$router.push({
                name: "bigModel",
                params: {
                  baseData: this.inputTextBaseData,
                },
              });
            });
        } else {
          return false;
        }
      });
    },
    remenber(data) {
      this.checked = data;
      if (this.checked) {
        localStorage.setItem("user", JSON.stringify(this.form));
      } else {
        localStorage.removeItem("user");
      }
    },
    forgetpas() {
      this.$message({
        type: "info",
        message: "功能尚未开发额😥",
        showClose: true,
      });
    },
    register() {
      this.$message({
        type: "info",
        message: "功能尚未开发额😥",
        showClose: true,
      });
    },
  },
};
</script>
  
  <style scoped>
.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  /* background-image: url("../assets/login2.jpg"); */
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
    font-weight: bolder;
    font-size: 15px;
    text-align: left;
  }
  
  /deep/ .el-button {
    width: 100%;
    margin-bottom: 10px;
  
  } */
</style>
  
  